package com.fy.market.strategy.service.raffle.template;

import com.fy.market.strategy.model.entity.*;
import com.fy.market.strategy.model.entity.tree.RuleTreeResultEntity;
import com.fy.market.strategy.model.entity.tree.valobj.RuleTreeVO;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.rule.chain.ILogicChain;
import com.fy.market.strategy.service.rule.chain.chainFactory.LogicChainFactory;
import com.fy.market.strategy.service.rule.tree.treeFactory.LogicTreeFactory;
import com.fy.market.strategy.service.rule.tree.treeFactory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 默认抽奖策略
 *
 * @author fwq
 * @date 2025-09-02
 * @desc 默认抽奖策略
 */
@Slf4j
@Service
public class DefaultRaffleStrategy extends AbstractRaffleStrategy {

    private LogicChainFactory logicChainFactory;

    private LogicTreeFactory logicTreeFactory;

    public DefaultRaffleStrategy(IStrategyRepository iStrategyRepository, LogicChainFactory logicChainFactory, LogicTreeFactory logicTreeFactory) {
        super(iStrategyRepository);
        this.logicChainFactory = logicChainFactory;
        this.logicTreeFactory = logicTreeFactory;
    }

    /**
     * 抽奖前 - 规则责任链过滤
     * @param ruleMatterEntity 规则物料实体类
     * @return 抽奖结果
     */
    @Override
    protected RuleChainResultEntity doCheckRaffleBeforeLogic(RuleMatterEntity ruleMatterEntity) {
        ILogicChain iLogicChain = logicChainFactory.assembleLogicChain(ruleMatterEntity.getStrategyId());
        return iLogicChain.logic(ruleMatterEntity.getUserId(), ruleMatterEntity.getStrategyId());
    }

    /**
     * 抽奖后 - 决策树过滤
     * @param ruleMatterEntity 规则物料实体类
     * @return 最终结果
     */
    @Override
    protected RuleTreeResultEntity doCheckRaffleAfterLogic(RuleMatterEntity ruleMatterEntity, RuleTreeVO ruleTreeVO) {
        IDecisionTreeEngine iDecisionTreeEngine = logicTreeFactory.openLogicTree(ruleTreeVO);
        RuleTreeResultEntity ruleTreeResultEntity = iDecisionTreeEngine.process(ruleMatterEntity);
        return ruleTreeResultEntity;
    }

}
